package com.darrenchan.leetcode;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-01-27 16:49
 */
public class QLint92 {
    //能否用前i个物品，组合拼出重量w
    public int backPack(int m, int[] A) {
        if (A == null || A.length == 0) {
            return 0;
        }
        int len = A.length;
        boolean[][] dp = new boolean[len + 1][m + 1];

        dp[0][0] = true;
        for (int i = 1; i <= len; i++) {
            for (int j = 0; j <= m; j++) {
                dp[i][j] = dp[i - 1][j];
                if (j >= A[i - 1]) {
                    dp[i][j] = dp[i][j] || dp[i - 1][j - A[i - 1]];
                }
            }
        }

        int res = 0;
        for (int i = m; i >= 0; i--) {
            if (dp[len][i]) {
                res = i;
                break;
            }
        }
        return res;
    }
}
